package com.batch.job;

import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobParameter;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.StepContribution;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepScope;
import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.FilterType;

import java.util.Map;


// 主要学习熟悉 JobParameter 的概念(学习章节2.1)
// 学习当前Job，请将@SpringBootApplication 取消注释，并将其他Job的@SpringBootApplication 方法注释,否则会对当前结果产生干扰

/**
 * 在idea启动参数中，自定义ParamJob的启动参数，Spring会自动将key-value封装为JobParameter对象
 * 启动参数添加样例： name=magicXd01
 *
 * 一、获取 JobParameter 参数两种途径：
 * 1、在Tasklet中，jobParameters = chunkContext.getStepContext().getJobParameters();
 * 2、 @Value("#{jobParameters['name']}")
 *    @StepScope 当调用bean时再加载，而不是创建时加载
 */


//@SpringBootApplication
// 因为用的Step路径在别的目录下，因此需要手动加ComponentScan
@ComponentScan(basePackages = "com.batch",
        excludeFilters = @ComponentScan.Filter(type = FilterType.REGEX, pattern = "com.batch.job"))
// EnableBatchProcessing 使spring创建springbatch操作相关类对象
@EnableBatchProcessing
public class ParamJob1 {

    @Autowired
    private ApplicationContext context;

    @Autowired
    private StepBuilderFactory stepBuilderFactory;
    // Job构建工厂，用于构建Job对象
    @Autowired
    private JobBuilderFactory jobBuilderFactory;

    // JobParameter
    private JobParameter jobParameter;

    @Bean
    public Tasklet paramTasklet1_01() {
        return (StepContribution contribution, ChunkContext chunkContext) -> {
            Map<String, Object> jobParameters = chunkContext.getStepContext().getJobParameters();
            Object name = jobParameters.get("name");
            System.out.println("chunkContext方式");
            if(name == null ){
                System.out.println("获取的jobParameters为空！");
                return RepeatStatus.FINISHED;
            }
            if(name instanceof String){
                System.out.println("获取的jobParameters为:"+name);
                return RepeatStatus.FINISHED;
            }else {
                System.out.println("获取的jobParameters类型不正确:"+name);
                return RepeatStatus.CONTINUABLE;
            }
        };
    }

    // 【重要】 延时获取
    @StepScope
    @Bean
    public Tasklet paramTasklet1_02(@Value("#{jobParameters['name']}")String name) {
        return (StepContribution contribution, ChunkContext chunkContext) -> {
            System.out.println("@Value方式：");
            if(name == null ){
                System.out.println("获取的jobParameters为空！");
                return RepeatStatus.FINISHED;
            }
            if(name instanceof String){
                System.out.println("获取的jobParameters为:"+name);
                return RepeatStatus.FINISHED;
            }else {
                System.out.println("获取的jobParameters类型不正确:"+name);
                return RepeatStatus.CONTINUABLE;
            }
        };
    }

    @Bean
    public Step paramStep1_01(){
        return stepBuilderFactory.get("param-step1_01").tasklet(paramTasklet1_01()).build();
    }
    @Bean
    public Step paramStep1_02(){
        return stepBuilderFactory.get("param-step1_01").tasklet(paramTasklet1_02(null)).build();
    }

    @Bean
    public Job paramJob1_01() {
        return jobBuilderFactory.get("param-job1_01").start(paramStep1_01()).build();
    }

    @Bean
    public Job paramJob1_02() {
        return jobBuilderFactory.get("param-job1_02").start(paramStep1_02()).build();
    }

    public static void main(String[] args) {
        SpringApplication.run(ParamJob1.class, args);
    }


}
